
namespace Pixels.Core
{
    using System;
    using System.Collections.Generic;
    using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
    using Pixels.Exceptions;
    using Pixels.ExtentedListItems;
    using Pixels.ExtentedListItems.DataLayer;

    internal class ExtentedListItemAdapter
    {
        #region private variables
        private DataAccessLayer dataLayer;
        private static object synclock = new object();
        private static ExtentedListItemAdapter _extentedlistitemadapter = null; 
        #endregion

        #region Constructors
        private ExtentedListItemAdapter()
        {
            dataLayer = new DataAccessLayer();
        }
        #endregion

        #region Methods

        public static ExtentedListItemAdapter GetInstance()
        {
            lock (synclock) // thread safe
            {
                if (_extentedlistitemadapter == null)
                {
                    _extentedlistitemadapter = new ExtentedListItemAdapter();
                }
            }
            return _extentedlistitemadapter;
        }

        public IExtentedListItem ExtentedListItemAddEdit(IExtentedListItem objExtentedListItem)
        {
            try
            {
                return dataLayer.ExtentedListItemAddEdit(objExtentedListItem);
            }
            catch (DLWrappedException Ex)
            {
                throw Ex;
            }
            catch (Exception Ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(Ex, "Business Layer Policy");
                throw new BLWrappedException("ExtentedListItem could not be added/modified due to invalid data/technical issues.");
            }
        }

        public void ExtentedListItemDelete(Guid ExtentedListItemID)
        {
            try
            {
                dataLayer.ExtentedListItemDelete(ExtentedListItemID);
            }
            catch (DLWrappedException Ex)
            {
                throw Ex;
            }
            catch (Exception Ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(Ex, "Business Layer Policy");
                throw new BLWrappedException("ExtentedListItem could not be deleted/Invalid ActivityID.");
            }
        }

        public IExtentedListItem ExtentedListItemFetch(Guid ExtentedListItemID)
        {
            try
            {
                IExtentedListItem objExtentedListItem = dataLayer.ExtentedListItemFetch(ExtentedListItemID);
                return objExtentedListItem;
            }
            catch (DLWrappedException Ex)
            {
                throw Ex;
            }
            catch (Exception Ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(Ex, "Business Layer Policy");
                return null;
            }
        }
        
        public IList<IExtentedListItem> ExtentedListItemsFetch()
        {
            try
            {
                IList<IExtentedListItem> ExtentedListItems = dataLayer.ExtentedListItemsFetch();
                return ExtentedListItems;
            }
            catch (DLWrappedException Ex)
            {
                throw Ex;
            }
            catch (Exception Ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(Ex, "Business Layer Policy");
                throw new BLWrappedException("Error in Fetching ExtentedListItem Detail.");
            }
        }      

        public IList<IExtentedListItem> ExtentedListItemsFetchByListItemID(Guid ListItemID)
        {
            try
            {
                if (ListItemID != null)
                {
                    IList<IExtentedListItem> ExtentedListItems = dataLayer.ExtentedListItemsFetchByListItemID(ListItemID);
                    return ExtentedListItems;
                }
                else
                    return null;
            }
            catch (DLWrappedException Ex)
            {
                throw Ex;
            }
            catch (Exception Ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(Ex, "Business Layer Policy");
                throw new BLWrappedException("Error in Fetching ExtentedListItem Detail.");
            }
        }

        public IList<IExtentedListItem> ExtentedListItemsFetchByContentItemID(Guid ContentItemID)
        {
            try
            {
                if (ContentItemID != null)
                {
                    IList<IExtentedListItem> ExtentedListItems = dataLayer.ExtentedListItemsFetch();
                    return ExtentedListItems;
                }
                else
                    return null;
            }
            catch (DLWrappedException Ex)
            {
                throw Ex;
            }
            catch (Exception Ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(Ex, "Business Layer Policy");
                throw new BLWrappedException("Error in Fetching ExtentedListItem Detail.");
            }
        }

        #endregion
    }
}
